import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiTrainingDatasetCategoriesApi } from '#/api/ai/dataset/trainingDatasetCategories';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { getTrainingDatasetCategoriesList } from '#/api/ai/dataset/trainingDatasetCategories';
import { ColorPicker } from '#/components/color-picker';
import { DictBadge } from '#/components/dict-badge';
import DictTag from '#/components/dict-tag/dict-tag.vue';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'parentId',
      label: '上级数据集分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getTrainingDatasetCategoriesList({});
          data.unshift({
            id: 0,
            name: '顶级分类',
          });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级数据集分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'code',
      label: '分类编码',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入数据集分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入数据集分类名称',
      },
    },
    {
      fieldName: 'description',
      label: '分类描述',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入数据集分类描述',
      },
    },
    {
      fieldName: 'icon',
      label: '图标',
      component: 'IconPicker',
      componentProps: {
        placeholder: '请选择分类图标',
        prefix: 'carbon',
      },
    },
    {
      fieldName: 'color',
      label: '颜色',
      component: 'ColorPicker',
      componentProps: {
        placeholder: '请输入颜色',
        showAlpha: false,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '分类编码',
      field: 'code',
    },
    {
      label: '分类名称',
      field: 'name',
    },
    {
      label: '父级分类',
      field: 'parentName',
    },
    {
      label: '分类描述',
      field: 'description',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '图标',
      field: 'icon',
      render: (val, data) => {
        return val
          ? h(IconifyIcon, {
              icon: val,
              color: data?.color,
            })
          : '无';
      },
    },
    {
      label: '颜色',
      field: 'color',
      render: (val) => {
        return val
          ? h(ColorPicker, {
              modelValue: val,
              disabled: true,
            })
          : '无';
      },
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        }),
    },
    {
      label: '排序',
      field: 'sort',
    },
    {
      label: '分类路径',
      field: 'codePath',
    },
    {
      label: 'ID路径',
      field: 'path',
    },
    {
      label: '系统内置',
      field: 'systemFlag',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '数据集个数',
      field: 'datasetCount',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '分类编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入数据集分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入数据集分类名称',
      },
    },
    {
      fieldName: 'parentId',
      label: '父级分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getTrainingDatasetCategoriesList({});
          data.unshift({
            id: 0,
            name: '顶级分类',
          });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级数据集分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      defaultValue: CommonStatusEnum.ENABLE,
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AiTrainingDatasetCategoriesApi.TrainingDatasetCategories>['columns'] {
  return [
    {
      field: 'id',
      title: '数据集分类id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'name',
      title: '分类名称',
      minWidth: 200,
      treeNode: true,
      align: 'left',
    },
    {
      field: 'code',
      title: '分类编码',
      minWidth: 100,
    },
    {
      field: 'parentId',
      title: '父级分类ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'description',
      title: '数据集分类描述',
      minWidth: 300,
    },
    {
      field: 'icon',
      title: '图标',
      minWidth: 60,
      slots: { default: 'icon' },
    },
    {
      field: 'color',
      title: '颜色',
      minWidth: 80,
      slots: { default: 'color' },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'codePath',
      title: '编码路径',
      minWidth: 200,
    },
    {
      field: 'systemFlag',
      title: '系统内置',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'datasetCount',
      title: '数据集个数',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 280,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
